package com.atguigu.guli.service.oss.controller;

import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.oss.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.concurrent.TimeUnit;

@RestController

@RequestMapping("/admin/oss/file")
public class FileController {
    @Autowired
    FileService fileService;
    @Value("${server.port}")
    Integer port;
    //测试远程 访问
    @PostMapping("test")
//    public R test(@RequestParam String uname ,@RequestParam String pwd){
//    public R test(@PathVariable String uname ,@PathVariable String pwd){
    public R test(@RequestBody R r){
//        System.out.println("uname = " + uname);
//        System.out.println("pwd = " + pwd);
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("r = " + r);
        System.out.println("端口号为："+ port + " FileController test方法被访问了");
        return R.ok();
    }


    //1、处理文件上传
    @PostMapping("upload")
    //文件表单项name值必须为file形参file才可以接收
    public R upload(MultipartFile file , String module){//module代表 oss桶中保存文件的目录WW
        String path = fileService.upload(file,module);
        return R.ok().setData("path" , path);
    }
    //2、删除文件
    @DeleteMapping("delete")
    public R delete(@RequestParam String path , @RequestParam String module){
        fileService.delete(path , module);
        return R.ok();
    }
}
